package net.e4net.watrix.action.login;

import java.io.*;

import java.io.IOException;



import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.e4net.watrix.ActionResponse;
import net.e4net.watrix.ResponseData;
import net.e4net.watrix.WatrixContext;
import net.e4net.watrix.WatrixException;
import net.e4net.watrix.common.hao.RequestParser;
import net.e4net.watrix.common.tao.TaoConnection;
import net.e4net.watrix.common.tao.TaoDataSet;
import net.e4net.watrix.common.tao.TaoResult;
import net.e4net.watrix.example.base.ExamConstants;
import net.e4net.watrix.base.member.UserVO;
import net.e4net.watrix.runtime.AbstractAction;
import net.e4net.watrix.runtime.ActionException;

public class LoginActn extends AbstractAction{

	
	@SuppressWarnings("unchecked")
	public ActionResponse execute(WatrixContext context, HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException,
			WatrixException {
		
		ResponseData data = super.getResponseData(request);
		TaoConnection con = null;
		String responsekey = "default";
		RequestParser parser = context.getRequestParser("default", request, response);
				
		String user_id = parser.getParameter("ID");
		String user_pw = parser.getParameter("PASSWD");
		TaoResult result = null;
		
		try{
			con = context.getTaoConnection("default", null);
			TaoDataSet dataSet = TaoDataSet.getInstance();
			dataSet.setString("ID", user_id);
			result = con.execute("net.e4net.watrix.proc.login.LoginProc", dataSet);
			
			if(result.getString("ID")!=null){
				if(user_pw.equals(result.getString("PASSWD")) ){
					data.put("result", result);
					
					HttpSession session = request.getSession();
					UserVO user = new UserVO();
					user.setUser_id(result.getString("ID"));
					user.setUser_code(result.getString("MCODE"));
					user.setUser_nm(result.getString("NAME"));
					user.setIsLogin(true);
					
					session.setAttribute("User", user);
										
				}
				else{
					data.put("errorcode", "passwordError");
					responsekey = "error";
					System.out.println("password1 test error");
				}
				
			}else{
				System.out.println("test id error");
				data.put("errorcode", "IDError");
				responsekey = "error";
				System.out.println("test id error2");
			}
			
		}catch(Throwable t){
			throw new ActionException(this.getActionDesc(context),
					context.getMessage(ExamConstants.SYSTEM_ERROR,null),t);
		}finally{
			try{con.close();}catch(Throwable ignore){}
		}
		
		
		return super.getActionResponse(context, responsekey);
	}

}
